/********************************************************************************/
/*   The  Software  is  proprietary,  confidential,  and  valuable to Realtek   */
/*   Semiconductor  Corporation  ("Realtek").  All  rights, including but not   */
/*   limited  to  copyrights,  patents,  trademarks, trade secrets, mask work   */
/*   rights, and other similar rights and interests, are reserved to Realtek.   */
/*   Without  prior  written  consent  from  Realtek,  copying, reproduction,   */
/*   modification,  distribution,  or  otherwise  is strictly prohibited. The   */
/*   Software  shall  be  kept  strictly  in  confidence,  and  shall  not be   */
/*   disclosed to or otherwise accessed by any third party.                     */
/*   c<2003> - <2012>                                                           */
/*   The Software is provided "AS IS" without any warranty of any kind,         */
/*   express, implied, statutory or otherwise.                                  */
/********************************************************************************/

//----------------------------------------------------------------------------------------------------
// ID Code      : RL6449_Series_ColorDCRLocalDimmingSetTCon.c
// Update Note  :
//----------------------------------------------------------------------------------------------------
#include "RL6449_Series_ColorLibInternalInclude.h"

//****************************************************************************
// DEFINITIONS / MACROS
//****************************************************************************
#define _FUNC_DLY          113
#define _TCON_INTER_DLY    7


//****************************************************************************
// STRUCT / TYPE / ENUM DEFINITTIONS
//****************************************************************************


//****************************************************************************
// CODE TABLES
//****************************************************************************


//****************************************************************************
// VARIABLE DECLARATIONS
//****************************************************************************


//****************************************************************************
// FUNCTION DECLARATIONS
//****************************************************************************
void ScalerColorDCRLocalDimmingSetTCon(EnumLocalDimmingHDRMode enumLocalDimmingHDRMode);


//****************************************************************************
// FUNCTION DEFINITIONS
//****************************************************************************
//--------------------------------------------------
// Description  : Read DCR Local Dimming Information Result
// Input Value  : Pointor of Local Dimming Information Result
// Output Value : none
//--------------------------------------------------
void ScalerColorDCRLocalDimmingSetTCon(EnumLocalDimmingHDRMode enumLocalDimmingHDRMode)
{
    WORD usHTotal = g_stMDomainOutputData.usHTotal;
    WORD usHWidth = g_stMDomainOutputData.usHBackWidth;
    WORD usHStart = g_stMDomainOutputData.usHBackStart;
    WORD usHStep = 0;
    WORD usHEnd = 0;

    WORD usVStart = g_stMDomainOutputData.usVBackStart;
    WORD usVStep = g_stMDomainOutputData.usVBackHeight;
    WORD usVEnd = 0;

    // Check even/odd mode
    if(ScalerFmtCnvGetHalfDataPathEvenOddModeStatus() == _FALSE)
    {
        usHStart = ((DWORD)usHTotal * usVStart / 2 + usHStart / 2) - (usHWidth + _FUNC_DLY);
        usVStart = (usHStart / usHTotal) * 2;
        usHStart = ((usHStart % usHTotal) - _TCON_INTER_DLY) * 2;
        usHStep = usHWidth / 4;
        ScalerSetDataPortBit(P15_A3_TCON_ADDR, _P15_A4_PT_B8_TC10_CTRL, ~_BIT3, _BIT3);
        ScalerSetDataPortBit(P15_A3_TCON_ADDR, _P15_A4_PT_C8_TC11_CTRL, ~_BIT3, _BIT3);
    }
    else
    {
        usHStart = ((DWORD)usHTotal * usVStart + usHStart) - (usHWidth + _FUNC_DLY);
        usVStart = usHStart / usHTotal;
        usHStart = (usHStart % usHTotal) - _TCON_INTER_DLY;
        usHStep = usHWidth / 8;
        ScalerSetDataPortBit(P15_A3_TCON_ADDR, _P15_A4_PT_B8_TC10_CTRL, ~_BIT3, 0x00);
        ScalerSetDataPortBit(P15_A3_TCON_ADDR, _P15_A4_PT_C8_TC11_CTRL, ~_BIT3, 0x00);
    }

    usHEnd = usHStart + usHStep;
    usVEnd = usVStart + usVStep;

    ScalerTcon10Adjust(usHStart % usHTotal, (usHStart + usHWidth) % usHTotal, usVStart + (usHStart / usHTotal), usVEnd + ((usHStart + 7 * usHStep) / usHTotal));
    ScalerTcon11Adjust((usHStart + usHWidth) % usHTotal, (usHStart + (usHWidth << 1)) % usHTotal, usVStart + (usHStart / usHTotal) + (((usHStart % usHTotal) + (usHStep << 2)) / usHTotal), usVEnd + ((usHStart + 7 * usHStep) / usHTotal));

    if(enumLocalDimmingHDRMode == _LOCAL_DIMMING_1X8_MODE)
    {
        ScalerTcon12Adjust(usHStart % usHTotal, (usHStart + usHWidth) % usHTotal, usVStart + (usHStart / usHTotal), usVEnd + ((usHStart + 7 * usHStep) / usHTotal));
        ScalerTcon13Adjust((usHStart + usHWidth) % usHTotal, (usHStart + (usHWidth << 1)) % usHTotal, usVStart + (usHStart / usHTotal), usVEnd + ((usHStart + 7 * usHStep) / usHTotal));
    }
    else
    {
        ScalerTcon12Adjust(usHStart % usHTotal, (usHStart + usHWidth) % usHTotal, usVStart + (usHStart / usHTotal), usVStart + (usVStep >> 1));
        ScalerTcon13Adjust((usHStart + usHWidth) % usHTotal, (usHStart + (usHWidth << 1)) % usHTotal, usVStart + (usHStart / usHTotal), usVStart + (usVStep >> 1));
    }

    ScalerTcon0Adjust(usHStart % usHTotal, usHEnd % usHTotal, usVStart + (usHStart / usHTotal), usVEnd + (usHStart / usHTotal));

    usHStart = usHEnd;
    usHEnd = usHStart + usHStep;

    ScalerTcon1Adjust(usHStart % usHTotal, usHEnd % usHTotal, usVStart + (usHStart / usHTotal), usVEnd + (usHStart / usHTotal));

    usHStart = usHEnd;
    usHEnd = usHStart + usHStep;

    ScalerTcon2Adjust(usHStart % usHTotal, usHEnd % usHTotal, usVStart + (usHStart / usHTotal), usVEnd + (usHStart / usHTotal));

    usHStart = usHEnd;
    usHEnd = usHStart + usHStep;

    ScalerTcon3Adjust(usHStart % usHTotal, usHEnd % usHTotal, usVStart + (usHStart / usHTotal), usVEnd + (usHStart / usHTotal));

    usHStart = usHEnd;
    usHEnd = usHStart + usHStep;

    ScalerTcon5Adjust(usHStart % usHTotal, usHEnd % usHTotal, usVStart + (usHStart / usHTotal), usVEnd + (usHStart / usHTotal));

    usHStart = usHEnd;
    usHEnd = usHStart + usHStep;

    ScalerTcon6Adjust(usHStart % usHTotal, usHEnd % usHTotal, usVStart + (usHStart / usHTotal), usVEnd + (usHStart / usHTotal));

    usHStart = usHEnd;
    usHEnd = usHStart + usHStep;

    ScalerTcon7Adjust(usHStart % usHTotal, usHEnd % usHTotal, usVStart + (usHStart / usHTotal), usVEnd + (usHStart / usHTotal));

    usHStart = usHEnd;
    usHEnd = usHStart + usHStep;

    ScalerTcon9Adjust(usHStart % usHTotal, usHEnd % usHTotal, usVStart + (usHStart / usHTotal), usVEnd + (usHStart / usHTotal));

    ScalerTcon0Enable(_ON);
    ScalerTcon1Enable(_ON);
    ScalerTcon2Enable(_ON);
    ScalerTcon3Enable(_ON);
    ScalerTcon5Enable(_ON);
    ScalerTcon6Enable(_ON);
    ScalerTcon7Enable(_ON);
    ScalerTcon9Enable(_ON);
    ScalerTcon10Enable(_ON);
    ScalerTcon11Enable(_ON);
    ScalerTcon12Enable(_ON);
    ScalerTcon13Enable(_ON);

    // global enable
    ScalerSetDataPortBit(P15_A3_TCON_ADDR, _P15_A4_PT_00_TC_CTRL0, ~_BIT6, _BIT6);
    ScalerTconGlobalEnable(_ON);
}
